﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using _2DGame_Preview_Server.Services.RoomAndBattle.Room.Modle;
using Assets.Script.Human.Player.Config;
using Script.NetSystem.Core;
using Script.NetSystem.Services.RoomAndBattle.RoomService;
using Script.UiSystem.Hall;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Script.UiSystem.Room
{
    public class RoomView : MonoBehaviour
    {
        private GameObject playerModel;
        private Transform playerContent;

        private List<PlayerModel> models;
        private PlayerInfo_Room[] playerInfoRooms;
        
        private TextMeshProUGUI button;
        private bool hasReady;
        private RoomClient roomClient;

        private void Start()
        {
            playerModel = Resources.Load<GameObject>("Hall&Room/PlayerCard");
        }

        private void OnDestroy()
        {
            roomClient.CloseConnect();
        }

        public void Init(RoomClient e)
        {
            roomClient = e;
        }


        public void LoadPlayerCard(List<PlayerInfo_Room> players)
        {
            playerInfoRooms = players.ToArray();
            hasReady = false;
            if (playerContent == null)
            {
                playerContent = GameObject.Find("PlayerContent").GetComponent<Transform>();
            }

            if (button == null)
            {
                button = GameObject.Find("BtnReady").GetComponentInChildren<TextMeshProUGUI>();
                button.text = hasReady ? PlayerModel.NotReady : PlayerModel.Ready;
            }
            
            models = new List<PlayerModel>();

            for (int i = playerContent.transform.childCount-1; i >=0 ; i--)
            {
                Destroy(playerContent.transform.GetChild(i).gameObject);
            }

            for (int i = 0; i < 3; i++)
            {
                GameObject go = Instantiate(playerModel, playerContent);
                PlayerModel pm = go.GetComponent<PlayerModel>();
                models.Add(pm);
                go.SetActive(false);
            }
            
            for (int i = 0; i < models.Count; i++)
            {
                if (i < players.Count)
                {
                    models[i].Init(players[i]);
                    models[i].gameObject.SetActive(true);
                }
                else
                {
                    models[i].gameObject.SetActive(false);
                }
            }
        }

        public RoleData[] GetPlayerRoleData()
        {
            RoleData[] roles = new RoleData[playerInfoRooms.Length];
            for (int i = 0; i < roles.Length; i++)
            {
                roles[i] = playerInfoRooms[i].roleData;
            }

            return roles;
        }
        
        public void UpdateStatus(int playerId, bool status)
        {
            foreach (var player in models)
            {
                if (player.PlayerId == playerId)
                {
                    player.UpdateStatus(status);
                    button.text = hasReady ? PlayerModel.NotReady : PlayerModel.Ready;
                    break;
                }
            }
        }

        public void DoReady()
        {
            hasReady = !hasReady;
            roomClient.ReadyGame(hasReady);
        }

        public void LeaveRoom(int playerId)
        {
            for (int i = 0; i < models.Count; i++)
            {
                if (models[i].PlayerId == playerId)
                {
                    Destroy(playerContent.transform.GetChild(i).gameObject);
                    break;
                }
            }
        }
    }


    public class RoomController
    {   
        public RoomView RoomView { get; set; }
        public RoomClient RoomClient { get; set; }

        public RoomController(int port)
        {
            GameObject go = new GameObject("RoomManager");
            RoomView = go.AddComponent<RoomView>();
            GameObject.DontDestroyOnLoad(go);

            RoomClient = new RoomClient(port, "连接房间", this, delegate
            {
                RoomClient.JoinRoom();
                SceneManager.LoadScene("RoomTemp");
            });

            RoomView.Init(RoomClient);
        }


        private void JoinRoom()
        {
            
        }

        public void UpdateReadyStatus(int playerId, bool ready)
        {
            RoomView.UpdateStatus(playerId, ready);
        }

        public void LeaveRoom(int playerId)
        {
            RoomView.LeaveRoom(playerId);
        }

        
    }
    
}